//
//  SCApiTool.m
//  Scalper
//
//  Created by 王云 on 2017/9/13.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SCApiTool.h"
#import "HWHTTPSessionManager.h"

NSString * const netFailMsg     = @"系统异常，请重新再试";

@implementation SCApiTool

#pragma mark post 方法
+ (void)ZYPost:(NSString *)urlString
    parameters:(NSDictionary *)parameters
 netIdentifier:(NSString *)netIdentifier
      progress:(void (^)(NSProgress *))progressBlock
       success:(void (^)(ZYJsonData *data))successBlock
       failure:(void (^)(NSString *))failureBlock
{
    
    
    if(kISHTTPLog){
        DLog(@" postDic =  %@",parameters);
    }
    
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
     [httpHelp.mager.requestSerializer setValue:@"application/vnd.cattlepie.v1+json" forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }

    
    [httpHelp post:[SCApiTool ZYPostUrl:urlString]
        parameters:parameters
     netIdentifier:netIdentifier
          progress:^(NSProgress *downloadProgress) {
              
              dispatch_async(dispatch_get_main_queue(), ^{
                  if (progressBlock) {
                      progressBlock(downloadProgress);
                  }
              });
              
          } success:^(id responseObject) {
              NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
              NSError *error;
              NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
              
              if(kISHTTPLog){
                  DLog(@"url = %@   responseObject =  %@",[SCApiTool ZYPostUrl:urlString],json);
              }
              ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
              if (data.code == 200) {
                  
                  if (successBlock) {
                      successBlock(data);
                  }
                  
              }else{
                  
                  if (failureBlock) {
                      failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                          [SCApiTool ZYPost:urlString parameters:parameters netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                      }]);
                  }
              }
              
          } failure:^(NSError *error) {
              if (failureBlock) {
                  failureBlock([SCApiTool operateFail:-1 msg:nil do:^{}]);
              }
          }];
    
}


#pragma mark get 方法
+  (void)ZYGet:(NSString *)urlString
 netIdentifier:(NSString *)netIdentifier
      progress:(void (^)(NSProgress *progress))progressBlock
       success:(void (^)(ZYJsonData *data))successBlock
       failure:(void (^)(NSString *fail))failureBlock
{
    
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:@"application/vnd.cattlepie.v1+json" forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }

    [httpHelp get:[SCApiTool ZYPostUrl:urlString]
       parameters:nil
    netIdentifier:netIdentifier
         progress:^(NSProgress *downloadProgress) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (progressBlock) {
                     progressBlock(downloadProgress);
                 }
             });
         } success:^(id responseObject) {
             
             NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
             NSError *error;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
             if(kISHTTPLog){
                 DLog(@"urlString = %@  responseObject =  %@",[SCApiTool ZYPostUrl:urlString],json);
             }
             ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
             if (data.code == 200) {
                 
                 if (successBlock) {
                     successBlock(data);
                 }
                 
             }else{
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                         [SCApiTool ZYGet:urlString netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                     }]);
                 }
             }
             
         } failure:^(NSError *error) {
             if (failureBlock) {
                 
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:-1 msg:nil do:^{
                         
                     }]);
                 }
                 
             }
         }];
    
    
    
    
}


#pragma mark  上传文件
+ (void)ZYUpload:(NSString *)urlString
      parameters:(NSDictionary *)parameters
     uploadParam:(NSArray *)uploadParams
   netIdentifier:(NSString *)netIdentifier
        progress:(void (^)(NSProgress *))progressBlock
         success:(void (^)(ZYJsonData *data))successBlock
         failure:(void (^)(NSString *fail))failureBlock
{
    
    
    NSMutableDictionary *postDic = [NSMutableDictionary dictionary];
    
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:@"application/vnd.cattlepie.v1+json" forHTTPHeaderField:@"Accept"];
    
    [httpHelp.mager.requestSerializer setValue:[parameters hw_safeObjectForKey:@"token"] forHTTPHeaderField:@"token"];

    NSMutableArray *newDataArr = [NSMutableArray array];
    for (NSDictionary *fileDic in uploadParams) {
        HWFormData *fromdata = [HWFormData new];
        fromdata.data = fileDic[@"data"];
        fromdata.name= fileDic[@"name"];
        fromdata.mimeType=fileDic[@"mimeType"];
        fromdata.filename = fileDic[@"filename"];
        [newDataArr addObject:fromdata];
    }
    
    [httpHelp upload:[SCApiTool ZYPostUrl:urlString]
          parameters:postDic
         uploadParam:newDataArr
       netIdentifier:netIdentifier
            progress:^(NSProgress *progress) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (progressBlock) {
                        progressBlock(progress);
                    }
                });
            } success:^(id responseObject) {
                
                
                NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
                NSError *error;
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
                
                if(kISHTTPLog){
                    DLog(@"parameters = %@  responseObject =  %@",postDic,json);
                }
                ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
                if (data.code == 200) {
                    
                    if (successBlock) {
                        successBlock(data);
                    }
                    
                }else{
                    
                    if (failureBlock) {
                        failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                            [SCApiTool ZYPost:urlString parameters:parameters netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                        }]);
                    }
                }
                
                
                
                
            } failure:^(NSError *error) {
                DLog(@"%@",error);
//                NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
//                NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//                NSLog(@"服务器的错误原因:%@",str);
                
                if (failureBlock) {
                    failureBlock([SCApiTool operateFail:-1 msg:nil do:^{
                        
                    }]);
                }
                
            }];
    
    
}




#pragma mark 入驻特殊POST 方法
+ (void)ZYPostStore_name:(NSString *)urlString
         parameters:(NSDictionary *)parameters
            token:(NSString *)token
      netIdentifier:(NSString *)netIdentifier
           progress:(void (^)(NSProgress *progress))progressBlock
            success:(void (^)(ZYJsonData *data))successBlock
            failure:(void (^)(NSString *msg))failureBlock{
    
    
    if(kISHTTPLog){
        DLog(@"urlString = %@ postDic =  %@",urlString,parameters);
    }
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:@"application/vnd.cattlepie.v1+json" forHTTPHeaderField:@"Accept"];
    [httpHelp.mager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    [httpHelp post:[SCApiTool ZYPostUrl:urlString]
        parameters:parameters
     netIdentifier:netIdentifier
          progress:^(NSProgress *downloadProgress) {
              
              dispatch_async(dispatch_get_main_queue(), ^{
                  if (progressBlock) {
                      progressBlock(downloadProgress);
                  }
              });
              
          } success:^(id responseObject) {
              NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
              NSError *error;
              NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
              if(kISHTTPLog){
                  DLog(@" responseObject =  %@",json);
              }
              ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
              if (data.code == 200) {
                  
                  if (successBlock) {
                      successBlock(data);
                  }
              }else{
                  
                  if (failureBlock) {
                      failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                          [SCApiTool ZYPost:urlString parameters:parameters netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                      }]);
                  }
              }
          } failure:^(NSError *error) {
              if (failureBlock) {
                  failureBlock([SCApiTool operateFail:-1 msg:nil do:^{}]);
              }
          }];
}


#pragma mark 带参数 get 方法
+  (void)ZYGet:(NSString *)urlString
    parameters:(NSDictionary *)parameters
 netIdentifier:(NSString *)netIdentifier
      progress:(void (^)(NSProgress *progress))progressBlock
       success:(void (^)(ZYJsonData *data))successBlock
       failure:(void (^)(NSString *msg))failureBlock{
    
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:@"application/vnd.cattlepie.v1+json" forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }
    
    [httpHelp get:[SCApiTool ZYPostUrl:urlString]
       parameters:parameters
    netIdentifier:netIdentifier
         progress:^(NSProgress *downloadProgress) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (progressBlock) {
                     progressBlock(downloadProgress);
                 }
             });
         } success:^(id responseObject) {
             
             NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
             NSError *error;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
             if(kISHTTPLog){
                 DLog(@"urlString = %@  parameters = %@ responseObject =  %@",[SCApiTool ZYPostUrl:urlString],parameters,json);
             }
             ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
             if (data.code == 200) {
                 
                 if (successBlock) {
                     successBlock(data);
                 }
                 
             }else{
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                         [SCApiTool ZYGet:urlString netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                     }]);
                 }
             }
             
         } failure:^(NSError *error) {
             if (failureBlock) {
                 
                   DLog(@"urlString = %@  parameters = %@ ",[SCApiTool ZYPostUrl:urlString],parameters);
                 
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:-1 msg:nil do:^{
                         
                     }]);
                 }
                 
             }
         }];
    
}

#pragma mark 二维码 get 方法
+  (void)ZYScanCodeGet:(NSString *)urlString
parameters:(NSDictionary *)parameters
 netIdentifier:(NSString *)netIdentifier
      progress:(void (^)(NSProgress *progress))progressBlock
       success:(void (^)(ZYJsonData *data))successBlock
       failure:(void (^)(NSString *msg))failureBlock{
    
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:@"application/vnd.cattlepie.v1+json" forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }
    [httpHelp get:urlString
       parameters:parameters
    netIdentifier:netIdentifier
         progress:^(NSProgress *downloadProgress) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (progressBlock) {
                     progressBlock(downloadProgress);
                 }
             });
         } success:^(id responseObject) {
             
             NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
             NSError *error;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
             if(kISHTTPLog){
                 DLog(@"urlString = %@   responseObject =  %@",[SCApiTool ZYPostUrl:urlString],json);
             }
             ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
             if (data.code == 200) {
                 
                 if (successBlock) {
                     successBlock(data);
                 }
                 
             }else{
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                         [SCApiTool ZYGet:urlString netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                     }]);
                 }
             }
             
         } failure:^(NSError *error) {
             if (failureBlock) {
                 
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:-1 msg:nil do:^{
                         
                     }]);
                 }
                 
             }
         }];
    

}




#pragma mark 服务器返回错误处理
+ (NSString *)operateFail:(NSInteger)status
                      msg:(NSString *)msg
                       do:(void (^)())done
{
    
    switch (status) {
        case -1://网络错误
        {
            return  @"网络错误，请重新再试！";
        }
            break;
        case 1001://tiket 过期
        {
            return netFailMsg;
        }
            break;
        case 405://token 过期 重新获取token，再继续上次请求
        {
            DLog(@"token过期..........token过期..........token过期..........token过期..........token过期..........token过期..........");
            [SCUserTool logOut];

            return  @"请重新登录";
        }
            break;
            
        case 2001://账号不存在
        {
            return msg==nil?@"账号不存在":msg;
        }
            
            
            
        default:
        {
            return msg==nil?netFailMsg:msg;
            
        }
            break;
    }
    return netFailMsg;
}




+ (NSString *)ZYPostUrl:(NSString *)urlString
{
    return [NSString stringWithFormat:@"%@%@",SCApiUrl,urlString];
}





#pragma mark 其他版本
#pragma mark
#pragma mark
#pragma mark

#pragma mark post 方法
+ (void)ZYVersionPost:(NSString *)urlString
               Accept:(NSString *)Accept
           parameters:(NSDictionary *)parameters
        netIdentifier:(NSString *)netIdentifier
             progress:(void (^)(NSProgress *progress))progressBlock
              success:(void (^)(ZYJsonData *data))successBlock
              failure:(void (^)(NSString *msg))failureBlock{
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:[NSString stringWithFormat:@"application/vnd.cattlepie.%@+json",Accept] forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }
    
    [httpHelp post:[SCApiTool ZYPostUrl:urlString]
        parameters:parameters
     netIdentifier:netIdentifier
          progress:^(NSProgress *downloadProgress) {
              
              dispatch_async(dispatch_get_main_queue(), ^{
                  if (progressBlock) {
                      progressBlock(downloadProgress);
                  }
              });
              
          } success:^(id responseObject) {
              NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
              NSError *error;
              NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
              
              if(kISHTTPLog){
                  DLog(@"urlStr = %@  parameters = %@  responseObject =  %@",[SCApiTool ZYPostUrl:urlString],parameters,json);
              }
              ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
              if (data.code == 200) {
                  
                  if (successBlock) {
                      successBlock(data);
                  }
                  
              }else{
                  
                  if (failureBlock) {
                      failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                          [SCApiTool ZYPost:urlString parameters:parameters netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                      }]);
                  }
              }
              
          } failure:^(NSError *error) {
              if(kISHTTPLog){
                  DLog(@"urlStr = %@  parameters = %@  ",[SCApiTool ZYPostUrl:urlString],parameters);
              }
              if (failureBlock) {
                  failureBlock([SCApiTool operateFail:-1 msg:nil do:^{}]);
              }
          }];
}


#pragma mark get 方法
+  (void)ZYVersionGet:(NSString *)urlString
               Accept:(NSString *)Accept
        netIdentifier:(NSString *)netIdentifier
             progress:(void (^)(NSProgress *progress))progressBlock
              success:(void (^)(ZYJsonData *data))successBlock
              failure:(void (^)(NSString *msg))failureBlock{
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:[NSString stringWithFormat:@"application/vnd.cattlepie.%@+json",Accept] forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }
    
    [httpHelp get:[SCApiTool ZYPostUrl:urlString]
       parameters:nil
    netIdentifier:netIdentifier
         progress:^(NSProgress *downloadProgress) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (progressBlock) {
                     progressBlock(downloadProgress);
                 }
             });
         } success:^(id responseObject) {
             
             NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
             NSError *error;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
             if(kISHTTPLog){
                 DLog(@"urlStr = %@    responseObject =  %@",[SCApiTool ZYPostUrl:urlString],json);
             }
             ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
             if (data.code == 200) {
                 
                 if (successBlock) {
                     successBlock(data);
                 }
                 
             }else{
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                         [SCApiTool ZYGet:urlString netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                     }]);
                 }
             }
             
         } failure:^(NSError *error) {
             if(kISHTTPLog){
                 DLog(@"urlStr = %@ ",[SCApiTool ZYPostUrl:urlString]);
             }
             if (failureBlock) {
                 
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:-1 msg:nil do:^{
                         
                     }]);
                 }
                 
             }
         }];
    
    
}

#pragma mark 带参数 get 方法
+  (void)ZYVersionGet:(NSString *)urlString
               Accept:(NSString *)Accept
           parameters:(NSDictionary *)parameters
        netIdentifier:(NSString *)netIdentifier
             progress:(void (^)(NSProgress *progress))progressBlock
              success:(void (^)(ZYJsonData *data))successBlock
              failure:(void (^)(NSString *msg))failureBlock{
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:[NSString stringWithFormat:@"application/vnd.cattlepie.%@+json",Accept] forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }
    
    [httpHelp get:[SCApiTool ZYPostUrl:urlString]
       parameters:parameters
    netIdentifier:netIdentifier
         progress:^(NSProgress *downloadProgress) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (progressBlock) {
                     progressBlock(downloadProgress);
                 }
             });
         } success:^(id responseObject) {
             
             NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
             NSError *error;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
             if(kISHTTPLog){
                 DLog(@"urlString = %@  parameters = %@ responseObject =  %@",[SCApiTool ZYPostUrl:urlString],parameters,json);
             }
             ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
             if (data.code == 200) {
                 
                 if (successBlock) {
                     successBlock(data);
                 }
                 
             }else{
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                         [SCApiTool ZYGet:urlString netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                     }]);
                 }
             }
             
         } failure:^(NSError *error) {
             if(kISHTTPLog){
                 DLog(@"urlStr = %@   parameters = %@",[SCApiTool ZYPostUrl:urlString],parameters);
             }
             if (failureBlock) {
                 
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:-1 msg:nil do:^{
                         
                     }]);
                 }
                 
             }
         }];
}

#pragma mark  上传文件
+ (void)ZYVersionUpload:(NSString *)urlString
                 Accept:(NSString *)Accept
             parameters:(NSDictionary *)parameters
            uploadParam:(NSArray *)uploadParams
          netIdentifier:(NSString *)netIdentifier
               progress:(void (^)(NSProgress *))progressBlock
                success:(void (^)(ZYJsonData *data))successBlock
                failure:(void (^)(NSString *fail))failureBlock{
    NSMutableDictionary *postDic = [NSMutableDictionary dictionary];
    
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:[NSString stringWithFormat:@"application/vnd.cattlepie.%@+json",Accept] forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }
    
    NSMutableArray *newDataArr = [NSMutableArray array];
    for (NSDictionary *fileDic in uploadParams) {
        HWFormData *fromdata = [HWFormData new];
        fromdata.data = fileDic[@"data"];
        fromdata.name= fileDic[@"name"];
        fromdata.mimeType=fileDic[@"mimeType"];
        fromdata.filename = fileDic[@"filename"];
        [newDataArr addObject:fromdata];
    }
    
    [httpHelp upload:[SCApiTool ZYPostUrl:urlString]
          parameters:postDic
         uploadParam:newDataArr
       netIdentifier:netIdentifier
            progress:^(NSProgress *progress) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (progressBlock) {
                        progressBlock(progress);
                    }
                });
            } success:^(id responseObject) {
                
                
                NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
                NSError *error;
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
                
                if(kISHTTPLog){
                    DLog(@"urlStr = %@  parameters = %@  responseObject =  %@",[SCApiTool ZYPostUrl:urlString],postDic,json);
                }
                ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
                if (data.code == 200) {
                    
                    if (successBlock) {
                        successBlock(data);
                    }
                    
                }else{
                    
                    if (failureBlock) {
                        failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                            [SCApiTool ZYPost:urlString parameters:parameters netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                        }]);
                    }
                }
                
                
                
                
            } failure:^(NSError *error) {
                if(kISHTTPLog){
                    DLog(@"urlStr = %@",[SCApiTool ZYPostUrl:urlString]);
                }
                if (failureBlock) {
                    failureBlock([SCApiTool operateFail:-1 msg:nil do:^{
                        
                    }]);
                }
                
            }];
    
}


#pragma mark 带参数 get 方法  url无须拼接
+  (void)ZYVersionScanCodeGet:(NSString *)urlString
                       Accept:(NSString *)Accept
                   parameters:(NSDictionary *)parameters
                netIdentifier:(NSString *)netIdentifier
                     progress:(void (^)(NSProgress *progress))progressBlock
                      success:(void (^)(ZYJsonData *data))successBlock
                      failure:(void (^)(NSString *msg))failureBlock{
    HWHTTPSessionManager *httpHelp = [HWHTTPSessionManager shareedHTTPSessionManager];
    httpHelp.mager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    httpHelp.mager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [httpHelp.mager.requestSerializer setValue:[NSString stringWithFormat:@"application/vnd.cattlepie.%@+json",Accept] forHTTPHeaderField:@"Accept"];
    SCUser *user = [SCUserTool loadInfo];
    if (user.isLogin) {
        [httpHelp.mager.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];
    }
    [httpHelp get:urlString
       parameters:parameters
    netIdentifier:netIdentifier
         progress:^(NSProgress *downloadProgress) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (progressBlock) {
                     progressBlock(downloadProgress);
                 }
             });
         } success:^(id responseObject) {
             
             NSString *responseStr =  [[ NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
             NSError *error;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
             if(kISHTTPLog){
                 DLog(@"urlString = %@   responseObject =  %@",[SCApiTool ZYPostUrl:urlString],json);
             }
             ZYJsonData *data = [ZYJsonData loadDataWithJson: json];
             if (data.code == 200) {
                 
                 if (successBlock) {
                     successBlock(data);
                 }
                 
             }else{
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:data.code msg:data.msg do:^{
                         [SCApiTool ZYGet:urlString netIdentifier:netIdentifier progress:progressBlock success:successBlock failure:failureBlock];
                     }]);
                 }
             }
             
         } failure:^(NSError *error) {
             if (failureBlock) {
                 
                 if (failureBlock) {
                     failureBlock([SCApiTool operateFail:-1 msg:nil do:^{
                         
                     }]);
                 }
                 
             }
         }];
    
}




@end
